package com.itheima.reggie.controller;



import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;


@RestController
@RequestMapping("/addressBook")
public class AddressBookController {

    @Autowired
    AddressBookService service;

    //保存地址
    @PostMapping
    public R saveAddressBook(@RequestBody AddressBook addressBook) {
        //设置当前地址对应的用户Id
        addressBook.setUserId(BaseContext.getCurrentId());
        service.save(addressBook);
        return R.success("保存地址成功");
    }

    //设置默认地址
    @PutMapping("/default")
    public R setDefaultAddress(@RequestBody AddressBook addressBook) {
        LambdaUpdateWrapper<AddressBook> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(AddressBook::getIsDefault, 0);
        wrapper.eq(AddressBook::getUserId, addressBook.getUserId());
        //设置当前地址对应的用户的所有地址都不是默认地址
        service.update(wrapper);
        addressBook.setIsDefault(1);
        //设置当前地址为默认地址
        service.updateById(addressBook);
        return R.success("设置成功");
    }

    //按用户id查询地址
    @GetMapping("/{id}")
    public R queryAddressById(@PathVariable Long id) {
        AddressBook byId = service.getById(id);
        if (byId != null) {
            return R.success(byId);
        }
        return R.error("没有找到该用户");
    }

    //查询用户的默认地址
    @GetMapping("/default")
    public R queryAddressDefault() {
        Long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AddressBook::getUserId, currentId);
        wrapper.eq(AddressBook::getIsDefault, 1);
        AddressBook one = service.getOne(wrapper);
        if (one != null) {
            return R.success(one);
        }
        return R.error("没有找到该对象");
    }

    //查询用户的所有地址
    @GetMapping("/list")
    public R queryAddressBook() {
        Long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(null != currentId, AddressBook::getUserId, currentId);
        wrapper.orderByDesc(AddressBook::getUpdateTime);
        List<AddressBook> list = service.list(wrapper);
        return R.success(list);
    }

    //删除地址
    @DeleteMapping
    public R deleteAddress(@RequestParam List<Long> ids) {
        service.removeByIds(ids);
        return R.success("删除成功");
    }

    //修改地址
    @PutMapping
    public R updateAddress(@RequestBody AddressBook addressBook) {

        LambdaUpdateWrapper<AddressBook> wrapper = new LambdaUpdateWrapper<AddressBook>();
        wrapper.eq(AddressBook::getId,addressBook.getId());
        wrapper.eq(AddressBook::getUserId,BaseContext.getCurrentId());
        service.update(addressBook,wrapper);
        return R.success("修改成功");
    }
}
